package com.zhoux.mongo.util;

import java.util.HashMap;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import net.sf.json.JSONArray;

public class JsonUtil {
	private static Log log=LogFactory.getLog(XmlUtil.class);
	private static JSONArray json=new JSONArray();

	public static String objectToString(Object obj){
		
		return json.element(obj).toString();
	}
	
	@SuppressWarnings({ "rawtypes", "static-access", "unchecked" })
	public static HashMap jsonToMap(String str){
		if(str.isEmpty()){
			return null;
		}
		JSONArray jsonStr=json.fromObject(str);
		List<HashMap> list= (List<HashMap>) json.toCollection(jsonStr, HashMap.class);
		if(list.isEmpty()){
			return null;
		}
		return list.get(0);
	}
	public static void main(String[] args) {
		String str="[{\"age\":25,\"name\":\"zhoux\"}]";
		jsonToMap(str);
	}
	
}
